<?php
/**
 * 商户订单核销端
 */
namespace app\index\controller;

use app\common\model\Order;
use app\common\org\Wxjsapi;
use think\Controller;
use think\Session;

class Bn extends Controller
{
    public function wx()
    {
        $echoStr = input('echostr','false');
        return $echoStr;
        exit;
    }

    // 核销首页
    public function index()
    {
        return $this->fetch();
    }

    // 核销登陆
    public function login()
    {
        $account = input('account','');
        $password = input('pwd','');
        if ($account == '123' && $password == '123') {
            Session::set('name',$account);
            return json(rm(0, '登陆成功'));
        }
        return json(rm(1, '登录失败'));
    }

    // 后台首页
    public function admin(){
        $name = Session::get('name');
        if(!$name){
            $this->redirect('/bn/index');
        }
        //获取wxjsdata
        $nowurl ="http://kshzj.iwawo.com/bn/admin";
        $Wxjsapi = new Wxjsapi();
        $Wxjsapi->getConfig($nowurl,$sharedata);
        $this->assign('share',$sharedata);
        return $this->fetch();
    }


    /**
     * @api {GET} /applet/use_goods 核销商品
     * @apiGroup 核销
     * @apiParamExample {json} 参数
     *  {
     *      "number":1        // 订单号
     * }
     * @apiSuccessExample {json} 返回
     * {
     *     "code": 1,
     *     "msg": "获取成功",
     *     "data": 'ok',
     *     "url": "",
     *     "wait": 3
     * }
     */
    public function useGoods()
    {
        $number = input('number');
        if ($number) {
            $check = Order::where(['number' => $number])->find();
            if ($check) {
                if($check['is_use'] == 0){
                    $check->save(['is_use' => 1, 'use_time' => time()]);
                    return json(rm(0, '核销成功'));
                }else{
                    return json(rm(1, '核销失败。订单已使用'));
                }
            }
            return json(rm(1, '订单号不存在'));
        }
        return json(rm(1, '订单号不能为空'));
    }
}